/**
 * 
 */
package co.fitstart.entity.coupon.stat;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import co.fitstart.entity.coupon.CouponBatch;
import co.fitstart.entity.coupon.CouponTemplate;

/**
 * 优惠券模版统计信息
 * @author mohanlan
 *
 */
@Entity
@Table(name = "coupon_template_stat")
@DynamicUpdate
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class CouponTemplateStat {

    @Id
    @GeneratedValue(generator = "generator")
    @GenericGenerator(name = "generator", strategy = "assigned")
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(unique= true)
    @JSONField(serialize = false)
    private CouponTemplate couponTemplate;

    /** 生成批次数 */
    private int batchCount;
    
    /** 总发放数 */
    private int totalCount;
    
    /** 总使用数 */
    private int usedCount;
    
    /** 过期数 */
    private int overdueCount;
    
    /** 总优惠金额 */
    private int amount;
    
    /** 实际优惠金额 */
    private int actualAmount;

    /** 创建日期(发布日期) */
    @Type(type="date")
    private Date createDate;
    
    /**
     * The default constructor
     */
    public CouponTemplateStat() {
        
    }
    
    /**
     * the constructor using coupon template as key
     * 
     * @param couponTemplate
     */
    public CouponTemplateStat(CouponTemplate couponTemplate) {
        this.id = couponTemplate.getId();
        this.couponTemplate = couponTemplate;
        this.createDate = new Date();
    }
    
    /**
     * 
     * @param usedCount
     * @param couponAmount
     * @return
     */
    public CouponTemplateStat useCoupon(int usedCount, int couponAmount) {
        this.usedCount = this.usedCount + usedCount;
        this.actualAmount = this.actualAmount + couponAmount;
        
        return this;
    }
    
    /**
     * 
     * @param couponBatch
     * @return
     */
    public CouponTemplateStat edit(CouponBatch couponBatch) {
        this.batchCount++;
        this.totalCount += couponBatch.getAmount();
        Integer amount = couponBatch.getCouponTemplate().getAmount();
        this.amount += amount == null ? 0 : amount * couponBatch.getAmount(); 
        return this;
    }
    
    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the couponTemplate
     */
    public CouponTemplate getCouponTemplate() {
        return couponTemplate;
    }

    /**
     * @return the batchCount
     */
    public int getBatchCount() {
        return batchCount;
    }

    /**
     * @return the totalCount
     */
    public int getTotalCount() {
        return totalCount;
    }

    /**
     * @return the usedCount
     */
    public int getUsedCount() {
        return usedCount;
    }

    /**
     * @return the overdueCount
     */
    public int getOverdueCount() {
        return overdueCount;
    }

    /**
     * @return the amount
     */
    public int getAmount() {
        return amount;
    }

    /**
     * @return the actualAmount
     */
    public int getActualAmount() {
        return actualAmount;
    }

    /**
     * @return the createDate
     */
    public Date getCreateDate() {
        return createDate;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param couponTemplate the couponTemplate to set
     */
    public void setCouponTemplate(CouponTemplate couponTemplate) {
        this.couponTemplate = couponTemplate;
    }

    /**
     * @param batchCount the batchCount to set
     */
    public void setBatchCount(int batchCount) {
        this.batchCount = batchCount;
    }

    /**
     * @param totalCount the totalCount to set
     */
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    /**
     * @param usedCount the usedCount to set
     */
    public void setUsedCount(int usedCount) {
        this.usedCount = usedCount;
    }

    /**
     * @param overdueCount the overdueCount to set
     */
    public void setOverdueCount(int overdueCount) {
        this.overdueCount = overdueCount;
    }

    /**
     * @param amount the amount to set
     */
    public void setAmount(int amount) {
        this.amount = amount;
    }

    /**
     * @param actualAmount the actualAmount to set
     */
    public void setActualAmount(int actualAmount) {
        this.actualAmount = actualAmount;
    }

    /**
     * @param createDate the createDate to set
     */
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

}
